<template>
  <div class='container'>
    <el-dialog
      :title="row.id?'编辑目录':'新增目录'"
      :visible.sync="addDialogVisible"
      width="20%"
      :before-close="handleClose">
      <el-form
        ref="form"
        :rules="addFormRules"
        :model="addForm"
        label-width="80px">
        <!-- 所属学科选择器 -->
        <el-form-item  label="所属学科">
          <el-select style="width: 100%;" v-model="addForm.subjectID" placeholder="请选择">
            <el-option
              v-for="item in simpleData"
              :key="item.value"
              :label="item.label"
              :value="item.value"
              @change="onChange"
              >
              </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="目录名称" prop="directoryName">
          <el-input v-model="addForm.directoryName"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="onSave">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { add, update } from '@/api/hmmm/directorys.js'
import { Message } from 'element-ui'
import { simple } from '@/api/hmmm/subjects.js'

export default {
  name: 'addDirectorys',
  model: {
    prop: 'addDialogVisible'
  },
  props: {
    addDialogVisible: {
      type: Boolean,
      required: true
    },
    row: {
      type: Object,
      required: true
    }
  },
  data () {
    return {
      addForm: {
        directoryName: this.row.id ? this.row.directoryName : '',
        subjectID: this.row.id ? this.row.subjectID : null
      },
      addFormRules: {
        directoryName: [
          { required: true, message: '请输入目录名称', trigger: 'blur' },
          { min: 2, max: 7, message: '长度在 2 到 7 个字符', trigger: 'blur' }
        ]
      },
      value: '',
      simpleData: []
    }
  },
  watch: {
    row: {
      handler (val) {
        this.addForm.directoryName = val === null ? '' : val.directoryName
        // console.log(val)
        this.addForm.subjectID = val === null ? '' : val.subjectID
      }
    }
  },
  created () {
    this.simpleList()
    // console.log(this.row)
  },

  methods: {
    // 对话框关闭
    handleClose () {
      // 重置表单
      this.$refs.form.resetFields()
      this.$emit('reset')
      this.$emit('input', false)
    },
    // 点击确定提交新增
    async onSave () {
      // 表单验证
      // await this.$refs.form.validate()
      if (this.row.id) {
        // 编辑
        await update({
          ...this.addForm,
          id: this.row.id
        })
        this.$message.success('修改成功')
        this.$emit('edit-success')
      } else {
        // 验证通过添加
        await this.$refs.form.validate()
        await add(this.addForm)
        Message.success('添加成功')
        this.$emit('add-success')
      }
      this.handleClose()
    },
    onChange () {

    },
    // 选择器简单列表
    async simpleList () {
      const res = await simple({
        subjectName: this.subjectName
      })
      // console.log(res)
      this.simpleData = res.data
    }
  }
}
</script>

<style scoped lang='less'>

</style>
